using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITS.DAL.Publications;
using ITS.Core.Domain.Publications;

namespace ITS.DAL.Concrete.EF.Publications
{

    public class EFPublicationRepository : IPublicationRepository
    {
        private readonly EnterpriseContext _context = new EnterpriseContext();
        public IQueryable<PublicationCategory> GetAllActiveCategories()
        {
            return _context.PublicationCategories;
        }

        public IQueryable<Publication> GetAllActivePublications()
        {
            return _context.Publications;
        }

        public Publication Get(Guid publicationID)
        {
            return _context.Publications.FirstOrDefault(x => x.PublicationID == publicationID);
        }

        public void Save(Publication o)
        {
            if (o.PublicationID.Equals(Guid.Empty))
            {
                o.PublicationID = Guid.NewGuid();
                _context.Publications.Add(o);
            }
            else
            {
                _context.Entry(o).State = System.Data.EntityState.Modified;
            }
            _context.SaveChanges();
        }
        public void Save()
        {
            _context.SaveChanges();
        }


        public IQueryable<int> GetPulicationYears()
        {
            return _context.Publications.Select(x => x.Year).Distinct();
        }


        public IQueryable<Publication> GetAllActivePublicationsByYear(int year)
        {
            return _context.Publications.Where(x => x.Active == true && x.Year == year);
        }
    }
}
